﻿using MultiDAQ_Analysis.Util;
using Newtonsoft.Json;
using System;
using System.IO;

namespace MultiDAQ_Analysis.Model
{
    public class GlobalPresetData
    {
        [JsonProperty("sampleDiskArea")]
        public double SampleDiskArea { get; set; } = 200; // 样品盘面积 mm²

        [JsonProperty("geometryFactor")]
        public double GeometryFactor { get; set; } = 0.9; // 几何因子
    }

    public static class GlobalPresetManager
    {
        private static string GetConfigFilePath()
        {
            string appPath = ChannelDataManager.GetDataDirectoryPath();
            return Path.Combine(appPath, "global_preset.json");
        }

        public static GlobalPresetData LoadGlobalPreset()
        {
            try
            {
                string filePath = GetConfigFilePath();
                if (File.Exists(filePath))
                {
                    string jsonContent = File.ReadAllText(filePath);
                    if (!string.IsNullOrWhiteSpace(jsonContent))
                    {
                        var data = JsonConvert.DeserializeObject<GlobalPresetData>(jsonContent);
                        if (data != null) return data;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"加载全局预设失败: {ex.Message}");
            }

            return new GlobalPresetData(); // 返回默认值
        }

        public static bool SaveGlobalPreset(GlobalPresetData data)
        {
            try
            {
                string filePath = GetConfigFilePath();
                string jsonContent = JsonConvert.SerializeObject(data, Formatting.Indented);
                File.WriteAllText(filePath, jsonContent);
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"保存全局预设失败: {ex.Message}");
                return false;
            }
        }
    }
}